package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.MenuTypeItemAdapter;
import com.dlc.felear.xiaoensale.entity.DeviceInfo;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.utils.InputMethodManagerUtil;

import java.util.List;


public class SelectGridviewDialog extends Dialog implements AdapterView.OnItemClickListener {
    private static final String TAG = "SelectGridviewDialog";
    private GridView gv;
    private Context context;
    private AppCompatActivity activity;
    private SelectDialogCallback callback;
    private List<String> tempList;
    private MenuTypeItemAdapter adapter;

    public SelectGridviewDialog(@NonNull Context context, SelectDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }


    public SelectGridviewDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        this.activity = (AppCompatActivity) context;
        View root = LayoutInflater.from(context).inflate(R.layout.double_select_gridview_dialog, null);
        gv = root.findViewById(R.id.gv);
        adapter = new MenuTypeItemAdapter(context);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
    }


    public void showDialog(List<String> list) {
        tempList = list;
        adapter.setItems(list);
        show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


    private void confirmData() {
        dismiss();
    }


    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            String value = tempList.get(position);
            if (callback != null) {
                callback.onItemClickResult(value);
            }
            confirmData();
        } catch (Exception e) {
            showToast(e.getMessage());
            confirmData();
        }

    }

    public interface SelectDialogCallback {
        void onItemClickResult(String result);
    }
}
